package com.mud.mudcommune.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;
import android.widget.RelativeLayout.LayoutParams;

import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.ProcessEntry;

import org.w3c.dom.Text;

/**
 * Created by welcome to china on 2016/12/6.
 */

public class ProcessAdapter extends BaseAdapter {
    private Context context;
    private List<ProcessEntry> list;

    public ProcessAdapter(Context context, List<ProcessEntry> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position) == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.process_item, parent, false);
            holder.ll_ex = (LinearLayout) convertView.findViewById(R.id.ll_ex);
            holder.content = (TextView) convertView
                    .findViewById(R.id.tv_title);
            holder.line = convertView.findViewById(R.id.v_line);
            holder.title = (RelativeLayout) convertView
                    .findViewById(R.id.rl_title);
            holder.titletext= (TextView) convertView.findViewById(R.id.txt_date_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 时间轴竖线的layout
        LayoutParams params = (LayoutParams) holder.line.getLayoutParams();
        // 第一条数据，肯定显示时间标题
        if (position == 0) {
            holder.title.setVisibility(View.VISIBLE);
            params.addRule(RelativeLayout.ALIGN_TOP, R.id.order_yes);
            params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.ll_ex);
        } else { // 不是第一条数据
            holder.title.setVisibility(View.VISIBLE);
            params.addRule(RelativeLayout.ALIGN_TOP, R.id.order_yes);
            params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.ll_ex);
        }
        if(position==list.size()-1){
            holder.line.setVisibility(View.INVISIBLE);
        }
        holder.line.setLayoutParams(params);
        holder.titletext.setText(list.get(position).getTitle());
        holder.content.setText(list.get(position).getText());
        return convertView;
    }

    public static class ViewHolder {
        RelativeLayout title;
        TextView titletext;
        View line;
        TextView content;
        LinearLayout ll_ex;
    }
}